Questions
21 of 45
1What is a pseudo-element in CSS?
2How are pseudo-elements different from pseudo-classes?
3What is the syntax of a pseudo-element?
4Name some commonly used pseudo-elements.
5What does the ::before pseudo-element do? What does the ::after pseudo-element do?
6How can you insert content using pseudo-elements?
7What is the difference between :before and ::before? Why were pseudo-elements changed from one colon (:) to two (::) in CSS3?
8What does the ::first-letter pseudo-element do?
9What does the ::first-line pseudo-element do?
10How can you style the first line of a paragraph differently?
11Can pseudo-elements be styled with animations or transitions?
12What’s the difference between using ::before and ::after?
13Can pseudo-elements be positioned using CSS positioning properties? Can pseudo-elements have background images or colors? How can you use pseudo-elements to create shapes or icons?
14Can you use multiple pseudo-elements on the same element?
15What is the ::selection pseudo-element used for?
16How does stacking and z-index affect pseudo-elements?
17Can pseudo-elements have child elements or content inside them?
18Can you apply pseudo-elements to replaced elements (like <img>)?
19How do pseudo-elements interact with display and overflow properties?
20What’s the difference between using pseudo-elements and real elements for decoration?
21How does inheritance work with pseudo-elements?
22Can pseudo-elements trigger JavaScript events (like click)?
23How can you control the generated content with pseudo-elements using attr()?
24Can pseudo-elements be used inside flex or grid layouts?
25How do you control the stacking order of ::before and ::after pseudo-elements?
26How can you create a tooltip using pseudo-elements?
27How can you make a custom underline or highlight effect using ::after?
28How can you make a quotation mark appear before a paragraph using pseudo-elements?
29How do you create a triangle or arrow shape using only CSS pseudo-elements?
30How can you use pseudo-elements to add icons without extra markup?
31How can you create a button hover animation using ::before or ::after?
32How can pseudo-elements help in implementing skeleton loaders or shimmer effects?
33How can you use ::before and ::after to clear floats?
34How can you create a border animation using pseudo-elements?
35How can pseudo-elements be used in responsive design?
36What is the ::marker pseudo-element and when is it used?
37What does the ::placeholder pseudo-element do?
38How does ::cue work with media elements?
39What is ::backdrop, and where is it used?
40What is the difference between ::file-selector-button and other input pseudo-elements?
41How does the ::part() pseudo-element work in Web Components?
42How is ::slotted() different from ::part() in shadow DOM styling?
43Can you style text highlights with pseudo-elements?
44How can ::selection be customized for accessibility and branding?
45What are the limitations of pseudo-elements compared to actual DOM elements?
21 / 45

How does inheritance work with pseudo-elements?

Inheritance in Pseudo-Elements in CSS

Pseudo-elements like ::before, ::after, ::first-letter, and ::first-line behave similarly to real elements in terms of CSS inheritance. They inherit properties from their parent element where applicable, but not all CSS properties are inherited by default.

Key Points
  1. 1

    Pseudo-elements inherit text-related properties such as color, font-family, font-size, and line-height from their parent element.

  2. 2

    Non-inheritable properties, like margin, padding, border, or background, must be explicitly set on the pseudo-element.

  3. 3

    Some pseudo-elements, like ::first-letter and ::first-line, may have special inheritance rules defined by the CSS specification.

  4. 4

    Using inherit explicitly in CSS allows you to force any property to inherit from the parent, even if it’s not naturally inheritable.

Example: Inheritance in ::before

In this example, the ::before pseudo-element inherits the color from the <p> element, so the star appears in teal. Properties like font-weight are explicitly set because they do not inherit by default.

Best Practices
  1. 1

    Understand which CSS properties are inherited and which are not when styling pseudo-elements.

  2. 2

    Use explicit declarations for non-inherited properties to ensure consistent rendering.

  3. 3

    Combine inherited and explicitly set properties for flexible, maintainable styling.

  4. 4

    Test across browsers to verify that inheritance behaves as expected for different pseudo-elements.

Difficulty: 8/10
Topics: pseudo-elements, inheritance, CSS specificity

Scenario Questions

0-2 years experience
  1. 1

    You're trying to add a star icon after a rating using ::after, but it's not showing up at all — what’s the most likely reason?

  2. 2

    I set font-size: 16px on a paragraph, and I expect the ::before icon to match it, but it’s tiny — why?

  3. 3

    I added content: '★' to a ::before, but it’s not aligning with the text — what CSS properties do I need to set explicitly?

2-5 years experience
  1. 1

    Our tooltip component uses ::after for the arrow, but when we switched to a new theme system, the arrow color broke — what’s likely going wrong with inheritance?

  2. 2

    A designer says the ::before badge on our buttons is inheriting the wrong background color — we didn’t set it explicitly. How do you debug this?

  3. 3

    We have a reusable component that uses ::after for a decorative separator, but it’s rendering differently in a modal vs. the main page — what CSS inheritance rules could be causing this?

5-8 years experience
  1. 1

    We’re building a scalable design system with pseudo-element icons, but we’re seeing performance jank on mobile when animating ::before/::after — how do you optimize this without losing flexibility?

  2. 2

    How would you design a CSS architecture that allows pseudo-elements to reliably inherit theme variables across nested components without requiring redundant declarations?

  3. 3

    A legacy component uses ::after for tooltips with hardcoded colors — we want to migrate to a token-based system. What are the risks, and how do you ensure backward compatibility?

8+ years experience
  1. 1

    We’re standardizing our UI across 12 product teams, and some are using pseudo-elements for critical UI cues — how do you enforce consistent inheritance behavior without over-constraining designers?

  2. 2

    Our CSS-in-JS library doesn’t support pseudo-element theming well — how would you architect a solution that allows dynamic pseudo-element styling via theme context without breaking SSR or hydration?

  3. 3

    A major accessibility audit found that pseudo-element icons used for status indicators aren’t exposed to screen readers — how do you redesign this at the architecture level to meet WCAG without rewriting every component?

Follow-up Questions

  • What happens if you try to set color on a parent but not on ::before?
  • Can you inherit font-size from a parent into a pseudo-element?
  • Why might a ::after element not respond to a CSS variable defined on its parent?